package com.business.common.dto;

import com.business.common.enums.BusinessExceptionEnum;
import com.business.common.enums.ErrorEnum;

import java.io.Serializable;

/**
 * 通用接口返回类
 * 默认拥有以下3个key
 * code ： 0 成功 其他失败
 * msg :错误消息描述
 * data:返回数据
 */
public class R<T> implements Serializable {

    public R() {

    }

    public R(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public R(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    private int code;

    private String msg;

    private T data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static R success() {
        return new R(ErrorEnum.SUCCESS.getCode(), ErrorEnum.SUCCESS.getMsg());
    }

    public static R success(Object data) {
        return new R(ErrorEnum.SUCCESS.getCode(), ErrorEnum.SUCCESS.getMsg(), data);
    }

    public static R error(int code, String msg) {
        return new R(code, msg);
    }

    public static R error(int code, String msg, Object data) {
        return new R(code, msg, data);
    }

    public static R error(ErrorEnum errorEnum) {
        return new R(errorEnum.getCode(), errorEnum.getMsg());
    }

    public static R error(ErrorEnum errorEnum, String detailed) {
        return new R(errorEnum.getCode(), errorEnum.getMsg() + "," + detailed);
    }

    public static R error(BusinessExceptionEnum errorEnum) {
        return new R(errorEnum.getCode(), errorEnum.getMsg());
    }

    public static R error(BusinessExceptionEnum errorEnum, String detailed) {
        return new R(errorEnum.getCode(), errorEnum.getMsg() + "," + detailed);
    }

    public static R error(BusinessExceptionEnum errorEnum, Object detailed) {
        return new R(errorEnum.getCode(), errorEnum.getMsg(), detailed);
    }
}
